package com.ziyucoding.springboot.labs.lab3.sca.rocketmqdemo.producerdemo.controller;

import com.ziyucoding.springboot.labs.lab3.sca.rocketmqdemo.producerdemo.message.Demo1Message;
import com.ziyucoding.springboot.labs.lab3.sca.rocketmqdemo.producerdemo.message.MySource;
import org.apache.rocketmq.common.message.MessageConst;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 控制器
 *
 * @author ziyucoding
 * @date 2022-10-24
 **/
@RestController
@RequestMapping("/demo1")
public class Demo1Controller {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private MySource mySource;

    @GetMapping("/send")
    public Map<String, Object> send() {
        // 创建 Message
        Demo1Message message = new Demo1Message(new Random().nextInt());
        // 创建 Spring Message 对象
        Message<Demo1Message> springMessage = MessageBuilder.withPayload(message)
                .build();
        // 发送消息
        boolean sendResult = mySource.demo1Output().send(springMessage);
        logger.info("[send][发送消息完成, 结果 = {}]", sendResult);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "发送消息成功");
        result.put("data", sendResult);
        return result;
    }

    @GetMapping("/send_delay")
    public Map<String, Object> sendDelay() {
        // 创建 Message
        Demo1Message message = new Demo1Message(new Random().nextInt());
        // 创建 Spring Message 对象
        // 设置延迟级别为 3，10 秒后消费。
        Message<Demo1Message> springMessage = MessageBuilder.withPayload(message)
                .setHeader(MessageConst.PROPERTY_DELAY_TIME_LEVEL, "3")
                .build();
        // 发送消息
        boolean sendResult = mySource.demo1Output().send(springMessage);
        logger.info("[sendDelay][发送消息完成, 结果 = {}]", sendResult);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "发送消息成功");
        result.put("data", sendResult);
        return result;
    }

    @GetMapping("/send_tag")
    public Map<String, Object> sendTag() {
        for (String tag : new String[]{"ziyu", "yutou"}) {
            // 创建 Message
            Demo1Message message = new Demo1Message(new Random().nextInt());
            // 创建 Spring Message 对象
            Message<Demo1Message> springMessage = MessageBuilder.withPayload(message)
                    .setHeader(MessageConst.PROPERTY_TAGS, tag) // 设置 Tag
                    .build();
            // 发送消息
            mySource.demo1Output().send(springMessage);
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "发送消息成功");
        result.put("data", true);
        return result;
    }
}
